package com.qf.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.constant.RabbitMQConstants;
import com.qf.service.SearchService;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author:z
 * time:2022/1/2 18:55
 * description:查询日志实现类
 */
@Service
public class SearchServiceImpl implements SearchService {

    @Resource
    ObjectMapper objectMapper;

    @Resource
    RestHighLevelClient restHighLevelClient;

    @Override
    public List<Map> searchLog(String paras) {
        //创建查询文档
        SearchRequest searchRequest = new SearchRequest();
        //
        searchRequest.indices(RabbitMQConstants.TOPIC_SMS_SEND_LOG);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        Map map = objectMapper.convertValue(paras, Map.class);
        String mobile = (String) map.get("mobile");
        if (!StringUtils.isEmpty(mobile)) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("mobile", mobile));
        }
        String keyword = (String) map.get("keyword");
        if (!StringUtils.isEmpty(keyword)) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("keyword", keyword));
        }
        Integer clientID = (Integer) map.get("clientID");
        if (clientID != null) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("clientID", clientID));
        }
        //开始时间
        long startTime = Long.parseLong(String.valueOf(map.get("startTime")));
        if(!StringUtils.isEmpty(startTime)){
            boolQueryBuilder.must(QueryBuilders.matchQuery("startTime",startTime));
        }
        //结束时间
        long endTime = Long.parseLong(String.valueOf(map.get("startTime")));
        if(!StringUtils.isEmpty(endTime)){
            boolQueryBuilder.must(QueryBuilders.matchQuery("endTime",endTime));
        }

//        //高亮前缀
//        String highLightPreTag = (String) map.get("highLightPreTag");
//        if (!StringUtils.isEmpty(highLightPreTag)) {
//            boolQueryBuilder.must(QueryBuilders.matchQuery("highLightPreTag", highLightPreTag));
//        }
//        //高亮前缀
//        String highLightPostTag = (String) map.get("highLightPostTag");
//        if (!StringUtils.isEmpty(highLightPostTag)) {
//            boolQueryBuilder.must(QueryBuilders.matchQuery("highLightPostTag", highLightPostTag));
//        }
        //短信内容
        String messageContent = (String) map.get("messageContent");
        if (!StringUtils.isEmpty(messageContent)) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("messageContent", messageContent));
        }


        //todo 条件筛选 补全
        searchSourceBuilder.query(boolQueryBuilder);
        searchRequest.source(searchSourceBuilder);
        List<Map> data = new ArrayList<>();
        try {
            SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            SearchHit[] hits = search.getHits().getHits();
            for (SearchHit hit : hits) {
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                data.add(sourceAsMap);
            }
            Map<String, Long> count = new HashMap<>();
            count.put("count", search.getHits().getTotalHits().value);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }
}
